package com.example.admin.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.admin.mapper.CategoryMapper;
import com.example.admin.service.CategoryService;
import com.example.xo.pojo.Category;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public boolean add(Category category) {
        //判断分类名是否重复，因为ID是雪花数，所以通过name判断
        boolean exist = ifNameExist(category);
        if (exist){
            return false;
        }

        //设置雪花ID
        String id = IdUtil.getSnowflakeNextIdStr();
        category.setCategoryId(id);

        //res->受影响的行数     增加成功为1      增加失败为0
        int res = categoryMapper.insert(category);
        return res==1;
    }



    //判断分类名是否存在
    private boolean ifNameExist(Category category) {
        Category categoryInDb = findByName(category.getCategoryName());
        if (categoryInDb!=null){
            log.info("该分类\"{}\"已存在", category.getCategoryName());
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteById(String id) {
        //todo 这里需要判断一次分类下面是否有博客，有的话不可以删除
        //软删除
        Category category=this.findById(id);
        //设置删除时间 LocalDateTime.now->当前时间
        category.setDeleted(LocalDateTime.now());

        //res->受影响的行数    0:没有被更改
        int res = categoryMapper.updateById(category);
        return res>0;
    }

    @Override
    public boolean update(Category category) {

        //判断修改后的分类名是否重复
        boolean exist = ifNameExist(category);
        if (exist){
            return false;
        }

        //更新修改时间
        category.setUpdated(LocalDateTime.now());

        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getCategoryId,category.getCategoryId());
        //判断是否软删除
        wrapper.isNull(Category::getDeleted);

        int res = categoryMapper.update(category,wrapper);
        return res>0;
    }

    @Override
    public List<Category> findAll() {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        //创建时间降序排序
        wrapper.orderByDesc(Category::getCreated);
        //判断是否被软删除    null则为未被软删除
        wrapper.isNull(Category::getDeleted);
        return categoryMapper.selectList(wrapper);
    }

    @Override
    public Category findById(String id) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        //验证ID
        wrapper.eq(Category::getCategoryId,id);
        //判断是否被软删除    null则为未被软删除
        wrapper.isNull(Category::getDeleted);
        //因为要判断是否被软删除，且有条件，只能查一个，所以是selectOne   不是selectById
        return categoryMapper.selectOne(wrapper);
        //return categoryMapper.selectById(id);
    }

    @Override
    public Category findByName(String name) {
        //条件构造器->容器
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        //对比name是否一致
        wrapper.eq(Category::getCategoryName,name);
        //判断是否软删除
        wrapper.isNull(Category::getDeleted);
        return categoryMapper.selectOne(wrapper);
    }
}
